########################################################################
######################## Regaining Control?  ###########################
######################## IO replication file ###########################
######################## Omer Solodoch       ###########################
########################################################################


# install.packages("tidyverse")
# install.packages("readxl")

library(tidyverse)
library(readxl)

####  Figure 1: google trends
Gtrends_bcontrols <- read_excel("C:/Users/LENOVO/Documents/R/data/dataFig1a.xlsx")
ggplot(Gtrends_bcontrols, aes(day, `Border Controls`)) + 
  geom_line(linetype = 1) +
  geom_point(size = 2) +
  geom_vline(xintercept=11, lty=2, lwd=1, col="red") +
  ggtitle("") +
  theme(plot.title = element_text(size = 10, face = "bold")) +
  scale_y_continuous(limits = c(0, 100)) +
  scale_x_continuous(breaks = Gtrends_bcontrols$day) +
  labs(y = "Relative intensity of searches",
       x = "Day of month, November 2015") +
  theme(axis.text=element_text(size=10),
        axis.title=element_text(size=10,face="bold")) 

Gtrends_EUTR <- read_excel("C:/Users/LENOVO/Documents/R/data/dataFig1b.xlsx")
ggplot(Gtrends_EUTR, aes(day, EUTR)) + 
  geom_line(linetype = 1) +
  geom_point(size = 2) +
  geom_vline(xintercept=18, lty=2, lwd=1, col="red") +
  ggtitle("") +
  theme(plot.title = element_text(size = 10, face = "bold")) +
  scale_y_continuous(limits = c(0, 100)) +
  scale_x_continuous(breaks = Gtrends_EUTR$day) +
  labs(y = "Relative intensity of searches",
       x = "Day of month, March 2016") +
  theme(axis.text=element_text(size=10),
        axis.title=element_text(size=10,face="bold")) 


####  Figure 3: Sweden and the EU
EB_ptrends2 <- read_excel("~/R/data/dataFig3.xlsx")
ggplot(EB_ptrends2, aes(Day, mean, col=Unit, shape = Unit)) + 
  geom_line(linetype = 2) +
  geom_errorbar(aes(ymin=mean - SE, ymax=mean + SE), 
                lwd=1, width=0) +
  geom_point(size = 2) +
  geom_vline(xintercept=-0.03, lty=2, lwd=1, col="grey54") +
  geom_segment(aes(x=-4,xend=0,y=36.683,yend=36.683), linetype = 1, size=1) +
  geom_segment(aes(x=0,xend=6,y=28.387,yend=28.387), linetype = 1, size=1) +
  ggtitle("") +
  theme(plot.title = element_text(size = 10, face = "bold")) +
  scale_y_continuous(limits = c(10, 75), breaks =c(10,20,30,40,50,60,70)) +
  labs(y = "Negatiev Feelings toward Non-EU Immigration (%)",
       x = "Days from Announcement of Border Controls in Sweden") +
  theme(legend.title=element_blank(), legend.position = c(0.11, 0.11),
        legend.background = element_blank(),
        panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank()) +
  scale_shape_manual(values = c(15, 16)) 


####  Figure 4: Border controls OLS estimates
SE_graph <- read_excel("~/R/data/dataFig4.xlsx")
ggplot(SE_graph, aes(level, coef)) + 
  geom_hline(yintercept=0, lty=2, lwd=0.7) +
  geom_errorbar(aes(ymin=coef - 1.96*SE, ymax=coef + 1.96*SE), 
                lwd=0.9, width=0) +
  geom_errorbar(aes(ymin=coef - 1.645*SE, ymax=coef + 1.645*SE), 
                lwd=1.5, width=0) +
  geom_point(size = 3) +
  ggtitle("") +
  theme(plot.title = element_text(size = 10, face = "bold")) +
  coord_flip() +
  scale_x_continuous(name = "", breaks = SE_graph$level, labels = SE_graph$var,
                     minor_breaks = seq(0, 9, 1)) +
  scale_y_continuous(name = "", limits=c(-0.1,0.2),
                     minor_breaks = seq(-0.1, 0.2, 0.1)) +
  labs(y = "") 


#### Figure 6: 
DE_graph_plcb <- read_excel("~/R/data/dataFig6.xlsx")

variable_names <- list(
  "1" = "All [-17,+13] days" ,
  "2" = "�3 days"
)


variable_labeller <- function(variable,value){
  return(variable_names[value])
}


ggplot(DE_effects_plcbo, aes(level, coef, color = Outcome, shape = Outcome)) + 
  geom_hline(yintercept=0, lty=2, lwd=0.7) +
  geom_errorbar(aes(ymin=coef - 1.96*SE, ymax=coef + 1.96*SE), 
                lwd=0.7, width=0) +
  geom_errorbar(aes(ymin=coef - 1.645*SE, ymax=coef + 1.645*SE), 
                lwd=1.3, width=0) +
  geom_point(size = 2.7, alpha = 0.7) +
  ggtitle("") +
  theme(plot.title = element_text(size = 10, face = "bold")) +
  coord_flip() +
  scale_x_continuous(name = "", breaks = DE_effects_plcbo$level, labels = DE_effects_plcbo$var,
                     minor_breaks = seq(0, 6, 1)) +
  scale_y_continuous(name = "", limits=c(-0.3,0.6),
                     minor_breaks = seq(-0.3, 0.6, 0.1)) +
  labs(y = "") +
  facet_grid(.~panel, labeller=variable_labeller) +
  scale_shape_manual(values = c(19,15)) +
  scale_color_manual(values = c("black","gray30")) +
  theme(legend.position = "none")

### Figure 7:
GIP_long_3days4w <- read_excel("C:/Users/LENOVO/Documents/R/data/dataFig7.xlsx")
ggplot(GIP_long_3days4w, aes(wave, AfD, col=Group)) + 
  geom_line(stat = "identity", alpha = 0.8,
            position = position_dodge(width = 0.09),
            aes(linetype = Group)) +
  geom_errorbar(stat = "identity", alpha = 0.8,
                position = position_dodge(width = 0.09),
                aes(ymin=AfD - SE*1.645, ymax=AfD + SE*1.645), 
                lwd=1.3, width=0) +
  geom_errorbar(stat = "identity", alpha = 0.8,
                position = position_dodge(width = 0.09),
                aes(ymin=AfD - SE*1.96, ymax=AfD + SE*1.96), 
                lwd=0.7, width=0) +
  geom_point(stat = "identity", alpha = 0.8,
             position = position_dodge(width = 0.09),
             aes(shape = Group), size = 3) +
  geom_vline(xintercept=3, lty=2, lwd=0.5, col="red") +
  ggtitle("") +
  theme(plot.title = element_text(size = 10, face = "bold")) +
  scale_y_continuous(limits = c(-5, 20)) +
  labs(y = "Will Vote for the AfD (%)",
       x = "") +
  scale_x_continuous(breaks = GIP_long_3days4w$wave, labels = GIP_long_3days4w$Date) +
  theme(legend.title=element_blank(), legend.position = c(0.11, 0.11),
        legend.background = element_blank()) +
  scale_shape_manual(values=c(15, 19)) +
  scale_color_manual(values=c("grey42", "black"))


####  Figure 8: SE over time
EB_long_SE <- read_excel("C:/Users/LENOVO/Documents/R/data/dataFig8.xlsx")
ggplot(EB_long_SE, aes(Wave, mean, shape=as.factor(Border))) + 
  geom_line() +
  geom_errorbar(stat = "identity", alpha = 0.8,
                position = position_dodge(width = 0.1),
                aes(ymin=mean - SE*1.96, ymax=mean + SE*1.96), 
                lwd=0.7, width=0) +
  geom_errorbar(stat = "identity", alpha = 0.8,
                position = position_dodge(width = 0.1),
                aes(ymin=mean - SE*1.645, ymax=mean + SE*1.645), 
                lwd=1.3, width=0) +
  geom_point(stat = "identity", alpha = 0.8,
             position = position_dodge(width = 0.1),
             aes(shape = as.factor(Border)), size = 3) +
  geom_vline(xintercept=3, lty=2, lwd=0.7, col="red") +
  ggtitle("") +
  theme(plot.title = element_text(size = 10, face = "bold")) +
  scale_y_continuous(limits = c(20, 45)) +
  labs(y = "Public Opposition to Non-EU Immigration (%)",
       x = "") +
  scale_x_continuous(breaks = EB_long_SE$Wave, labels = EB_long_SE$Date) +
  theme(legend.title=element_blank(), legend.position = c(0.11, 0.09),
        legend.background = element_blank()) +
  scale_shape_manual(labels = c("No Border Controls", "Border Controls"),
                     values=c(15, 19)) 


DE_long_reg <- read_excel("C:/Users/LENOVO/Documents/R/data/dataFig9.xlsx")

ggplot(DE_long_reg, aes(Wave, coef)) + 
  geom_errorbar(stat = "identity", alpha = 0.8,
                position = position_dodge(width = -0.06),
                aes(ymin=coef - SE*1.645, ymax=coef + SE*1.645), 
                lwd=1.3, width=0) +
  geom_errorbar(stat = "identity", alpha = 0.8,
                position = position_dodge(width = -0.06),
                aes(ymin=coef - SE*1.96, ymax=coef + SE*1.96), 
                lwd=0.7, width=0) +
  geom_point(stat = "identity", alpha = 0.8,
             position = position_dodge(width = -0.06),
             size = 3) +
  geom_hline(yintercept=0, lty=2, lwd=0.7) +
  geom_vline(xintercept=1.5, lty=2, lwd=1, col="red") +
  ggtitle("") +
  theme(plot.title = element_text(size = 10, face = "bold")) +
  scale_y_continuous(limits = c(-0.25, 0.1)) +
  labs(y = "",
       x = "") +
  scale_x_continuous(breaks = DE_long_reg$Wave, labels = DE_long_reg$date) +
  theme(axis.text=element_text(size=10),
        axis.title=element_text(size=10,face="bold"),
        axis.text.x = element_text(angle = 90, hjust = 1, size=9)) +
  facet_wrap(~Outcome)

######## Online Appendix

Press_Bcontrols <- read_excel("C:/Users/LENOVO/Documents/R/data/dataFigS11a.xlsx")
ggplot(Press_Bcontrols, aes(day, HITS)) + 
  geom_line(linetype = 1) +
  geom_point(size = 2) +
  geom_vline(xintercept=11, lty=2, lwd=1, col="red") +
  ggtitle("") +
  theme(plot.title = element_text(size = 10, face = "bold")) +
  scale_y_continuous(limits = c(0, 100)) +
  scale_x_continuous(breaks = Press_Bcontrols$day) +
  labs(y = "Number of Hits",
       x = "Day of month, November 2015") +
  theme(axis.text=element_text(size=10),
        axis.title=element_text(size=10,face="bold")) 


Press_EUTR <- read_excel("C:/Users/LENOVO/Documents/R/data/dataFigS11b.xlsx")
ggplot(Press_EUTR, aes(Day, Articles)) + 
  geom_line(linetype = 1) +
  geom_point(size = 2) +
  geom_vline(xintercept=18, lty=2, lwd=1, col="red") +
  ggtitle("") +
  theme(plot.title = element_text(size = 10, face = "bold")) +
  scale_y_continuous(limits = c(0, 40)) +
  scale_x_continuous(breaks = Press_EUTR$Day) +
  labs(y = "Number of Articles",
       x = "Day of month, March 2016") +
  theme(axis.text=element_text(size=10),
        axis.title=element_text(size=10,face="bold")) 

## Denmark
Gtrends_bcontrols_DK <- read_excel("C:/Users/LENOVO/Documents/R/data/dataFigS12.xlsx")

ggplot(Gtrends_bcontrols_DK, aes(day, `Border Controls`)) + 
  geom_line(linetype = 1) +
  geom_point(size = 2) +
  geom_vline(xintercept=11, lty=2, lwd=1, col="red") +
  ggtitle("") +
  theme(plot.title = element_text(size = 10, face = "bold")) +
  scale_y_continuous(limits = c(0, 100)) +
  scale_x_continuous(breaks = Gtrends_bcontrols_DK$day) +
  labs(y = "Relative intensity of searches",
       x = "Day of month, November 2015") +
  theme(axis.text=element_text(size=10),
        axis.title=element_text(size=10,face="bold")) 


################ asylum applications
Asylum_SEDE <- read_excel("C:/Users/LENOVO/Documents/R/data/dataFigS22.xlsx")

ggplot(Asylum_SEDE, aes(Month, Sweden)) + 
  geom_line(linetype = 1) +
  geom_point(size = 1) +
  geom_vline(xintercept=23, lty=2, lwd=1, col="red") +
  ggtitle("") +
  theme(plot.title = element_text(size = 10, face = "bold")) +
  scale_y_continuous(limits = c(0, 40000)) +
  labs(y = "First Time Asylum Applications",
       x = "Date") +
  scale_x_continuous(breaks = c(1,13,25,37, 49), labels = c("Jan-14", "Jan-15", "Jan-16", "Jan-17", "Jan-18")) +
  theme(axis.text=element_text(size=8),
        axis.title=element_text(size=8,face="bold")) 

ggplot(Asylum_SEDE, aes(Month, Germany)) + 
  geom_line(linetype = 1) +
  geom_point(size = 1) +
  geom_vline(xintercept=27, lty=2, lwd=1, col="red") +
  ggtitle("") +
  theme(plot.title = element_text(size = 10, face = "bold")) +
  scale_y_continuous(limits = c(0, 100000)) +
  labs(y = "First Time Asylum Applications",
       x = "Date") +
  scale_x_continuous(breaks = c(1,13,25,37, 49), labels = c("Jan-14", "Jan-15", "Jan-16", "Jan-17", "Jan-18")) +
  theme(axis.text=element_text(size=8),
        axis.title=element_text(size=8,face="bold")) 







